package be.kuleuven.cs.mop.gui.dialogs;


import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import be.kuleuven.cs.mop.app.controllers.Controllers;
import be.kuleuven.cs.mop.domain.exceptions.InvalidCurrentUserException;
import be.kuleuven.cs.mop.domain.model.Invitation;
import be.kuleuven.cs.mop.domain.model.Task;
import be.kuleuven.cs.mop.domain.model.User;
import be.kuleuven.cs.mop.gui.components.ActionButton;
import be.kuleuven.cs.mop.gui.components.ComboSelector;
import be.kuleuven.cs.mop.gui.components.SelectionList;
import be.kuleuven.cs.mop.gui.components.Wrapper;
import be.kuleuven.cs.mop.gui.util.Representation;
import be.kuleuven.cs.mop.gui.util.SwingUtils;



/**
 * {@link ActionDialog} for the "Manage Helper Invitations" use case
 */
public class ManageHelperInvitationsDialog extends ActionDialog {
	private static final long serialVersionUID = 1L;
	
	private Wrapper<SelectionList<Entry>>	invitations;
	private ComboSelector<Task>				tasks;
	private ComboSelector<User>				users;
	
	
	public ManageHelperInvitationsDialog(final Frame parent,
			final Controllers controllers) {
		super(parent, "Manage Helper Invitations", controllers);
		final GridBagConstraints gbc = SwingUtils.getGBC();
		
		try {
			invitations	= new Wrapper<SelectionList<Entry>>(
					new SelectionList<Entry>(
							new ArrayList<Entry>(), false));
			tasks		= new ComboSelector<Task>(
					getControllers().getControllerTasks().getUserTasks().toArray(
							new Task[0]));
			users		= new ComboSelector<User>(
					getControllers().getControllerUsers().getUsers().toArray(
							new User[0]));
		} catch (final InvalidCurrentUserException e) {
			log(e);
			dispose();
			return;
		}
		
		onSelect(tasks.getSelection());
		tasks.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				onSelect(tasks.getSelection());
			}
		});
		
		gbc.gridx	= 0;
		gbc.gridy	= 0;
		add(tasks, gbc);
		
		gbc.gridy	= 1;
		gbc.gridheight	= 2;
		add(invitations, gbc);
		
		gbc.gridx	= 1;
		gbc.gridy	= 0;
		gbc.gridheight	= 1;
		add(users, gbc);
		
		gbc.gridy	= 1;
		add(new ActionButton("Add", new Dimension(150, 32), this, "A"), gbc);
		
		gbc.gridy	= 2;
		add(new ActionButton("Remove", new Dimension(150, 32), this, "R"), gbc);
		
		display();
	}
	
	
	
	@Override
	protected void handle(final ActionEvent event) throws Exception {
		//	TODO
		switch (event.getActionCommand().charAt(0)) {
			case 'A':	//	Add
				getControllers().getControllerUsers().addUserInvitation(
						tasks.getSelection(),
						users.getSelection());
				break;
				
			case 'R':	//	Remove
				getControllers().getControllerUsers().removeUserInvitation(
						invitations.get().getSelection().get(0).get());
				break;
				
			default:	//	should never be reached
				throw new InternalError("Illegal parameter");
		}
	}
	
	protected void onSelect(final Task task) {
		try {
			invitations.set(new SelectionList<Entry>(
					Entry.wrap(getControllers().getControllerUsers()
							.getInvitations(task)), false));
		} catch (final Exception e) {
			//	ignored
		}
		
		invalidate();
		pack();
	}
	
	
	
	
	
	
	
	
	
	
	private static class Entry extends Representation<Invitation> {
		
		public Entry(final Invitation invitation) {
			super(invitation);
		}
		
		
		
		@Override
		public String toString() {
			return String.format("%s  (%s)  -  %s",
					get().getUser().getName(),
					get().getUser().getType().getName(),
					get().getStatus());
		}
		
		public static List<Entry> wrap(final List<Invitation> invitations) {
			final List<Entry> list = new ArrayList<Entry>();
			for (final Invitation invitation : invitations)
				list.add(new Entry(invitation));
			return list;
		}
		
	}
	
}